package com.example.home.hospital.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Locale;

public class AddCardActivity extends AppCompatActivity {

    private DatePicker datePicker;
    private Button buttonShowDatePicker;
    private TextView tv_date;

    private ImageView back;

    private EditText editTextName;

    private RadioGroup radioGroupGender;
    private RadioButton radioButtonMale;
    private RadioButton radioButtonFemale;

    private EditText editTextID;

    private EditText editTextPhone;

    private EditText editTextAddress;
    private Button buttonConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_card);
        initView();
        getAddData();
    }

    private void getAddData() {
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editTextName.getText().toString();
                int selectedGender = getSelectedGender();
                String carId = editTextID.getText().toString();
                String phone = editTextPhone.getText().toString();
                String address = editTextAddress.getText().toString();
                String date = tv_date.getText().toString();

                if (!TextUtils.isEmpty(address)) {
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("name", name);
                        jsonObject.put("sex", selectedGender);
                        jsonObject.put("cardId", carId);
                        jsonObject.put("tel", phone);
                        jsonObject.put("address", address);
                        jsonObject.put("birthday", date);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    OKHttpTool.doNetRequest("prod-api/api/hospital/patient",
                            OKHttpTool.POST,
                            GeneralClass.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    runOnUiThread(new Runnable() {
                                        GeneralClass generalClass = (GeneralClass) obj;
                                        @Override
                                        public void run() {
                                            if (generalClass.getCode() == 200) {
                                                Toast.makeText(AddCardActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                                AddCardActivity.this.finish();
                                            } else if (generalClass.getCode() == 500) {
                                                Toast.makeText(AddCardActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {
                                    Log.e("xhf", "网络访问失败");
                                }
                            }, jsonObject.toString());
                } else {
                    Toast.makeText(AddCardActivity.this, "信息不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {

        back = findViewById(R.id.back);

        editTextName = findViewById(R.id.editTextName);

        radioGroupGender = findViewById(R.id.radioGroupGender);
        radioButtonMale = findViewById(R.id.man);
        radioButtonFemale = findViewById(R.id.women);
        editTextID = findViewById(R.id.editTextID);
        editTextPhone = findViewById(R.id.editTextPhone);
        editTextAddress = findViewById(R.id.editTextAddress);
        buttonConfirm = findViewById(R.id.buttonConfirm);

        datePicker = findViewById(R.id.datePicker);
        buttonShowDatePicker = findViewById(R.id.buttonShowDatePicker);
        tv_date = findViewById(R.id.tv_date);

        buttonShowDatePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog();
            }
        });

        // 设置 RadioGroup 的选择变化监听器
        radioGroupGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 根据选中的 RadioButton 进行不同的操作
                if (checkedId == R.id.man) {
                    getSelectedGender();
                } else if (checkedId == R.id.women) {
                    getSelectedGender();
                }
            }
        });

        datePicker.setVisibility(View.GONE);
    }


    private int getSelectedGender() {
        int selectedGender = -1;
        int checkedRadioButtonId = radioGroupGender.getCheckedRadioButtonId();

        if (checkedRadioButtonId == R.id.man) {
            selectedGender = 1; // 假设0代表男性
        } else if (checkedRadioButtonId == R.id.women) {
            selectedGender = 0; // 假设1代表女性
        }

        return selectedGender;
    }

    private void showDatePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(
                AddCardActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 处理日期选择的逻辑，将选择的日期设置到相应的文本框中
                        updateSelectedDate(year, monthOfYear, dayOfMonth);
                    }
                },
                year, month, day);

        datePickerDialog.show();
    }

    private void updateSelectedDate(int year, int month, int day) {
        // 根据需要更新你的文本框，这里是一个示例
        String selectedDate = String.format(Locale.getDefault(), "%04d-%02d-%02d", year, month + 1, day);
        tv_date.setText(selectedDate);
        // 隐藏选择日期的按钮
        buttonShowDatePicker.setVisibility(View.VISIBLE);
    }
}
